package cn.jd.org.controller;

import cn.jd.basic.anno.LogAnno;
import cn.jd.basic.config.BussinessException;
import cn.jd.basic.util.AjaxResult;
import cn.jd.basic.util.PageList;
import cn.jd.org.domain.SysLogEntity;
import cn.jd.org.domain.Employee;
import cn.jd.org.query.SysLogEntityQuery;
import cn.jd.org.service.SysLogEntityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/log")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@LogAnno
public class SysLogEntityController {
    @Autowired
    private SysLogEntityService SysLogEntityService;
    @PutMapping
    public AjaxResult addSysLogEntity(@RequestBody SysLogEntity SysLogEntity) {
        try {
            if(SysLogEntity.getId() == null){
                SysLogEntityService.save(SysLogEntity);
                return AjaxResult.me().success();
            }else {
                SysLogEntityService.update(SysLogEntity);
                return AjaxResult.me().success();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
    @DeleteMapping("/{id}")
    @LogAnno(method = "deleteSysLogEntity", methodDes = "删除的方法")
    public  AjaxResult deleteSysLogEntity(@PathVariable("id") Long id){

        try {
            SysLogEntityService.remove(id);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().fail(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
/*    @DeleteMapping("/{id}")
    @LogAnno(method = "deleteSysLogEntity", methodDes = "删除的方法")
    public  AjaxResult deleteSysLogEntity(@PathVariable("id") Long id){

        try {
            SysLogEntityService.remove(id);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().fail(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }*/
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    public List<SysLogEntity> findall(){
        return SysLogEntityService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询一条数据" )
    public  SysLogEntity findall(@ApiParam(value="传入long类型的数据",required = true) @PathVariable("id") Long id){
        return SysLogEntityService.findById(id);
    }
    @PostMapping
    @LogAnno(method = "findall", methodDes = "分页查询的方法")
    public PageList<SysLogEntity> findall(@RequestBody SysLogEntityQuery query){

        return SysLogEntityService.queryByPage(query);
    }
    // 批量删除
    @PatchMapping
    public  AjaxResult deleteAll(@RequestBody List<Long> ids){

        try {
            SysLogEntityService.deleteByAll(ids);
            return AjaxResult.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

}
